# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libultrahdr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4.0
pkgrel=2
pkgdesc="Library for encoding and decoding ultrahdr images (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/google/libultrahdr"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-angleproject"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-winpthreads")
source=("https://github.com/google/libultrahdr/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-pkg-config.patch"
        "https://github.com/google/libultrahdr/commit/6db3a83ee2b1f79850f3f597172289808dc6a331.patch")
sha512sums=('2282bed4ae353f3b43039ca69b699754f9ef7e1611f321bf27ad1e50e7894953b8e44f28cbe215c186b5b037682a6d5733c6e7c638444d29fdcb5a661294c075'
            'ccaa15bc71c0c4a88044bd35ab929b88763cf20838f6e3b0bf7b3623ea851649e97fb0dac633a1160af805d63634a1f98e4f5f19415a7647d589f23c63f08add'
            '124e7a24857148f08862a3b6b32af69618076bae485e053e1446fb8592b0b3e449b1a66dc1c758aacbc8a4a97dc2e813b607875199809b5b7127db2f869f8d83')

prepare() {
  cd "${_realname}-${pkgver}"

  # https://github.com/google/libultrahdr/issues/289
  patch -Np1 -i "${srcdir}"/001-pkg-config.patch

  # https://github.com/google/libultrahdr/pull/356
  patch -Np1 -i "${srcdir}"/6db3a83ee2b1f79850f3f597172289808dc6a331.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -G Ninja \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${extra_config[@]}" \
    -DUHDR_ENABLE_GLES=ON \
    -S "${_realname}-${pkgver}" \
    -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
